<?php
/**
 * mailer.class
 * 
 * Caching mechanizm
 * 
 * @version:	0.2b
 * @package:	core
 * @author:	alex_ez
 * @created:	21 dec 2008
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */

if( defined( '__MAILER.CLASS_PHP' ) ) return;
define( '__MAILER.CLASS_PHP', '0.2b' );

class MailerException extends Exception { }

class mailer extends singleton
{
	// php singleton wrapper
	static public function &__getMe( ) { return parent::__getMe( __CLASS__ ); }
	static public function &instance() { return parent::__getMe(__CLASS__); }
	
	private $_dir = '/';
	private $_view = null;
	private $_config = null;
	
	// changeable
	private $_from = null;
	private $_host = null;
	private $_subject = null;
	
	
	protected function __construct( )
	{
		$this ->_config	= config::__getMe( );
		$this ->_view = view::__getMe( );
		
		$this ->_dir	= _::pick( $this ->_config ->mailer__templates, '' );
		
		$this ->host	= _::pick( $this ->_config ->mailer__host, $this ->_config ->host, @$_SERVER['SERVER_NAME'] );
		$this ->from	= _::pick( $this ->_config ->mailer__email, @$_SERVER['SERVER_ADMIN'], 'no-reply' );
		$this ->_subject = _::pick( $this ->_config ->mailer__subject, $this ->_config ->meta__title );
	}
	
	public function __set( $k, $v )
	{
		$k = @strtolower( $k );
		if( !$k || !strpos( ',from,host', $k ) ) return false;
		
		switch( $k )
		{
		case 'host':
			if( !texo::isDomain($v) ) throw new MailerException( "host ".$v." not valid" );
			$this ->_host = $v;
			$this ->from = 'no-reply';
		break;
		case 'from':
			if( !texo::isEMail($v) ) {
				if( !is_string( $v ) ) throw new MailerException( "from ".$v." not valid" );
				$v = texo::latinate( $v );
				$this ->_from = sprintf( '%s@%s', $v, $this ->_host );
				break;
			}
			$this ->_from = $v;
		break;
		default: return null;
		}
		
		return true;
	}
	
	public function __get( $k )
	{
		$k = @strtolower( $k );
		if( !$k || strpos( ',from,host', $k ) === false ) return false;
		
		switch( $k )
		{
		case 'host':	return $this ->_host;
		case 'from':	list( $r, ) = explode( '@', $this ->_from ); return $r;
		}
		return null;
	}
	
	// deprecated
	public function send( $o, $subject = null, $message = null )
	{
		// old style
		if( texo::isEMail( $o ) )
		{
			$o = array( 'to' => $o );
			if( !is_null( $subject ) ) $o['subject'] = $subject;
			if( !is_null( $message ) ) $o['body'] = $message;
		}
		
		// checkups
		if( !is_array($o) )		throw new MailerException( "where is parameters?" );
		if( !isset($o['to']) )		throw new MailerException( "sorry, who waits for a message?" );
		if( !texo::isEMail($o['to']) )	throw new MailerException( "to '{$o['to']}' not a valid email" );
		if( !texo::isEMail($this->_from) ) throw new MailerException( "from '{$this->_from}' not a valid email? oh... fuck!" );
		
		// default fields
		$_defaults = array(
			'template' => '_default.tpl',
			'subject' => $this ->_subject,
			);
		
		// fetch fields
		foreach( array( 'template', 'subject' ) as $k )
			if( !isset( $o[$k] ) ) $o[$k] = $_defaults[$k];
		foreach( array( 'from', 'host' ) as $k )
			if( !isset( $o[$k] ) ) $o[$k] = $this ->$k;
		
		$o['template'] = preg_replace( '/\.tpl$/i', '', $o['template'] ).'.tpl';
		fb::log( $o, 'mailer.send( options )' );
		
		// create body or fetch ready
		if( !isset( $o['body'] ) || !is_string( $o['body'] ) )
		{
			$templates = array_unique( array( $this ->from.'@'.$o['template'], $o['template'], $this ->from.'@'.'_default.tpl', '_default.tpl' ) );
			$templates = array_map( create_function( '$v', 'return "'.$this->_dir.'".$v;' ), $templates );
			fb::log( $templates );
			unset( $o['template'] );
			$body = $this ->_view ->display( $templates, $o );
			fb::log( $body, 'body' );
		}
		
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
		//$headers .= 'To: ' . $o['to'] . "\r\n";
		$headers .= 'From: ' . $this ->_from . "\r\n";
		
		//foreach( array( 'template', 'body', 'o' ) as $k ) fb::log( $$k, $k );
		fb::log( $headers.'To: ' . $o['to'], 'headers' );
		mail( $o['to'], $o['subject'], $body, $headers );
	}
}


?>